package cn.bjxy.shishaolong.talk_room.server;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.SimpleAttributeSet;

import cn.bjxy.shishaolong.talk_room.Utils;

public class ServerUI {

	private static final String SOFT_NAME = "聊天室—服务端";
	private static final String SOFT_VERTION = "V1.0.0";
	
	private JFrame frame;
	private JTextField ip_tField;
	private JTextField port_tField;
	private JTextArea showInfo_tArea;
	private JTable member_jTable;
	
	private static ServerService service = new ServerService();

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerUI window = new ServerUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 777, 577);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle(SOFT_NAME+SOFT_VERTION+"   -By时少龙");
		frame.getContentPane().setLayout(null);
		
		//显示信息区域
		showInfo_tArea = new JTextArea();
		showInfo_tArea.setFont(new Font("Monospaced", Font.PLAIN, 16));
//		showInfo_tArea.setBounds(222, 99, 509, 397);
		showInfo_tArea.setEditable(false);
		
		//显示信息文本区域的滚动条
		JScrollPane showInfo_jScroPane = new JScrollPane(showInfo_tArea);
		showInfo_jScroPane.setBounds(222, 99, 509, 397);
		showInfo_jScroPane.setAutoscrolls(true);
		frame.getContentPane().add(showInfo_jScroPane);
			
		//成员信息
		member_jTable = new JTable();
//		member_jTable.setBounds(29, 102, 145, 385);
//		frame.getContentPane().add(member_jTable);
		JScrollPane member_jScroPane = new JScrollPane(member_jTable);//成员信息滚动条
		member_jScroPane.setBounds(29, 102, 145, 385);
		frame.getContentPane().add(member_jScroPane);
		
		//IP地址 标签
		JLabel ip_label = new JLabel("IP地址：");
		ip_label.setBounds(29, 30, 61, 21);
		frame.getContentPane().add(ip_label);
		
		//填写ip地址的文本框
		ip_tField = new JTextField();
		ip_tField.setBounds(88, 27, 102, 24);
		frame.getContentPane().add(ip_tField);
		ip_tField.setColumns(10);
		ip_tField.setText(getLocalIpAddress());
		
		//端口号标签
		JLabel port_label = new JLabel("端口号：");
		port_label.setBounds(235, 33, 61, 21);
		frame.getContentPane().add(port_label);
		
		//填写端口号的文本框
		port_tField = new JTextField();
		port_tField.setColumns(10);
		port_tField.setBounds(294, 30, 102, 24);
		frame.getContentPane().add(port_tField);
		
		//开启服务的按钮
		final JButton startServ_btn = new JButton("开启服务");
		startServ_btn.setBounds(466, 27, 113, 27);
		frame.getContentPane().add(startServ_btn);
		
		//关闭服务的按钮
		final JButton closeServ_btn = new JButton("关闭服务");
		closeServ_btn.setBounds(595, 27, 113, 27);
		closeServ_btn.setEnabled(false);
		frame.getContentPane().add(closeServ_btn);
		
		//======================添加监听部分开始===========================
		//开启服务的监听
		startServ_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//界面组件锁定
				startLockUI(startServ_btn, closeServ_btn);
				//启动处理聊天服务
				new Thread(
						service.new HandlellerService(
								Integer.parseInt(port_tField.getText()),
								showInfo_tArea, member_jTable))
				.start();
				//界面显示开启服务
				showInfo_tArea.append(
						new StringBuilder()
						.append(Utils.dateFormat()).append(":\n")
						.append("服务端(")
						.append(ip_tField.getText())
						.append(":").append(port_tField.getText()).append(")启动成功,正在等待客户端连接...")
						.toString());
			}
		});
		//关闭服务的监听
		closeServ_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//启动关闭聊天服务
//				new Thread(service.new closeService()).start();
				service.closeService();
				//界面显示关闭服务
				showInfo_tArea.append(
						new StringBuilder("\n")
						.append(Utils.dateFormat()).append(":\n")
						.append("服务端已关闭").toString());
				//界面组件锁定
				closeLockUI(startServ_btn, closeServ_btn);
			}
		});		
	}
	//======================添加监听部分结束===========================
	
	//点击开启服务后组件状态的设置
	private void startLockUI(final JButton startServ_btn,final JButton closeServ_btn) {
		showInfo_tArea.setText("");
		ip_tField.setEditable(false);
		port_tField.setEditable(false);
		startServ_btn.setEnabled(false);
		closeServ_btn.setEnabled(true);
	}
	//点击关闭服务后组件状态的设置
	private void closeLockUI(final JButton startServ_btn,final JButton closeServ_btn) {
		ip_tField.setEditable(true);
		port_tField.setEditable(true);
		startServ_btn.setEnabled(true);
		closeServ_btn.setEnabled(false);
	}
	
	
	//获取到本机ip地址
	private String getLocalIpAddress() {
		String loopIp = "172.0.0.1";
		String ip = loopIp;
		try {
			InetAddress localHost = InetAddress.getLocalHost();
			ip = localHost.getHostAddress();
		} catch (UnknownHostException e) {
			ip = loopIp;
			e.printStackTrace();
		}
		return ip;
	}
}
